package com.sky.controller.admin;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加套餐")
    @CacheEvict(cacheNames = "setmealCach",key = "#setmealDTO.categoryId")
    public Result<String>SetmealAdd(@RequestBody SetmealDTO setmealDTO){
        log.info("添加套餐{}",setmealDTO);
        setmealService.SetmealAdd(setmealDTO);
        return Result.success("添加成功");
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> SetmealPageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.SetmealPageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> SetmealById(@PathVariable Long id){
        log.info("根据id查询套餐{}",id);
        SetmealVO setmealVO = setmealService.SetmealByid(id);
        return Result.success(setmealVO);
    }
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCach",allEntries = true)
    public Result<String> SetmealsDelete(@RequestParam List<Long> ids){
        log.info("批量删除套餐{}",ids);
        setmealService.SetmealDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改套餐停售与开售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("修改套餐停售与开售")
    @CacheEvict(cacheNames = "setmealCach",allEntries = true)
    public Result<String> IsSale(@PathVariable Integer status,
                                 @RequestParam Long id){
        log.info("修改套餐停售与开售");
        setmealService.IsSale(status,id);
        return Result.success("修改成功");
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "setmealCach",key = "#setmealDTO.categoryId")
    public Result<String> SetmealUpdate(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息{}",setmealDTO);
        setmealService.SetmealUpdate(setmealDTO);
        return Result.success("修改套餐信息");
    }
}
